<?php

class log {
	function __construct($args) {
		//Default values
		if(!isset($args['passive']))
			$args['passive'] = true;
		if(!isset($args['mode']))
			$args['mode'] = 'a';
		
		//Set arguments	
		$this->fn = $args['fn'];
		$this->passive = $args['passive'];
		$this->mode = $args['mode'];
		
		//If passive, don't open the file until log();
		if($this->passive) {
			$this->handler = false;
		} else {
			$this->open();
		}
	}
	
	//Open the file
	function open() {
		$this->handler = fopen($this->fn,$this->mode);
	}
	
	//Close the file
	function close() {
		fclose($this->handler);
		$this->handler = false;
	}
	
	//Log an entry
	function write($msg) {
		if($this->passive) //If passive, open up the file handler
			$this->open();
		
		fwrite($this->handler,time().' '.$msg.NL); //Log the error
		
		if($this->passive) //If passive, close the file handler
			$this->close();
	}
	
	function __destruct() {
		if($this->handler) //If handler is open, close it
			$this->close();
	}
};

//A blank log class, so nothing happens when 
class blankLog {
	function write($msg) {}
};

?>
